#include "include/hyn_core.h"
#include "stm32h7xx_hal.h"



#define EXAMPLE_PIN_NUM_SCL 13
#define EXAMPLE_PIN_NUM_SDA 14



static I2C_HandleTypeDef hi2c3;

static void MX_I2C3_Init(void);

void touch_iic_init(void)
{
#if 0
    i2c_master_bus_config_t i2c_mst_config_1 = {
        .clk_source        = I2C_CLK_SRC_DEFAULT,
        .i2c_port          = 1,
        .scl_io_num        = EXAMPLE_PIN_NUM_SCL,
        .sda_io_num        = EXAMPLE_PIN_NUM_SDA,
        .glitch_ignore_cnt = 7,

    };

    ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config_1, &bus_handle));

    i2c_device_config_t i2c_dev_conf = {
        .scl_speed_hz   = 100000,
        .device_address = 0x15,
    };

    if(i2c_master_bus_add_device(bus_handle, &i2c_dev_conf, &dev_handle) != ESP_OK)
    {
        printf("i2c dev add error\r\n");
    }
    i2c_device_config_t i2c_dev_conf2 = {
        .scl_speed_hz   = 100000,
        .device_address = 0x6A,
    };
    if(i2c_master_bus_add_device(bus_handle, &i2c_dev_conf2, &boot_dev_handle) != ESP_OK)
    {
        printf("i2c boot dev add error\r\n");
    }
#endif

    MX_I2C3_Init();
}

static int i2c_master_send(u8 addr, u8* buf, u16 len)
{
    // if(addr==0x15)
    //     handle = dev_handle;
    // else
    //     handle = boot_dev_handle;

    // esp_err_t ret = i2c_master_transmit(handle,buf,len,100);
    // return ret;

    int ret = -1;

    if(HAL_OK == HAL_I2C_Master_Transmit(&hi2c3, addr << 1, buf, len, 100))
    {
        ret = 0;
    }

    return ret;
}

static int i2c_master_recv(u8 addr, u8* buf, u16 len)
{
    // if(addr==0x15)
    //     handle = dev_handle;
    // else
    //     handle = boot_dev_handle;


    // esp_err_t ret = i2c_master_receive(handle,buf,len,100);
    // return ret;

    int ret = -1;
    if(HAL_OK == HAL_I2C_Master_Receive(&hi2c3, (addr << 1) | 1, buf, len, 100))
    {
        ret = 0;
    }
    return ret;
}


int hyn_write_data(struct hyn_ts_data* ts_data, u8* buf, u8 reg_len, u16 len)
{
    int ret = 0;
    ret     = i2c_master_send(ts_data->salve_addr, buf, len);
    return ret < 0 ? -1 : 0;
}

int hyn_read_data(struct hyn_ts_data* ts_data, u8* buf, u16 len)
{
    int ret = 0;
    ret     = i2c_master_recv(ts_data->salve_addr, buf, len);
    return ret < 0 ? -1 : 0;
}

int hyn_wr_reg(struct hyn_ts_data* ts_data, u32 reg_addr, u8 reg_len, u8* rbuf, u16 rlen)
{
    int ret = 0, i = 0;
    u8  wbuf[4];
    reg_len = reg_len & 0x0F;
    memset(wbuf, 0, sizeof(wbuf));
    i = reg_len;
    while(i)
    {
        i--;
        wbuf[i] = reg_addr;
        reg_addr >>= 8;
    }
    ret = i2c_master_send(ts_data->salve_addr, wbuf, reg_len);
    if(rlen)
    {
        ret |= i2c_master_recv(ts_data->salve_addr, rbuf, rlen);
    }
    return ret < 0 ? -1 : 0;
}



void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
    GPIO_InitTypeDef         GPIO_InitStruct     = {0};
    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
    if(hi2c->Instance == I2C3)
    {
        /* USER CODE BEGIN I2C3_MspInit 0 */

        /* USER CODE END I2C3_MspInit 0 */

        /** Initializes the peripherals clock
         */
        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C3;
        PeriphClkInitStruct.I2c123ClockSelection = RCC_I2C123CLKSOURCE_D2PCLK1;
        if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
        {
            //Error_Handler();
        }

        __HAL_RCC_GPIOC_CLK_ENABLE();
        __HAL_RCC_GPIOA_CLK_ENABLE();
        /**I2C3 GPIO Configuration
        PC9     ------> I2C3_SDA
        PA8     ------> I2C3_SCL
        */
        GPIO_InitStruct.Pin       = GPIO_PIN_9;
        GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull      = GPIO_NOPULL;
        GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
        HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

        GPIO_InitStruct.Pin       = GPIO_PIN_8;
        GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull      = GPIO_NOPULL;
        GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        /* Peripheral clock enable */
        __HAL_RCC_I2C3_CLK_ENABLE();
        /* USER CODE BEGIN I2C3_MspInit 1 */

        /* USER CODE END I2C3_MspInit 1 */
    }
}


static void MX_I2C3_Init(void)
{

    /* USER CODE BEGIN I2C3_Init 0 */

    /* USER CODE END I2C3_Init 0 */

    /* USER CODE BEGIN I2C3_Init 1 */

    /* USER CODE END I2C3_Init 1 */
    hi2c3.Instance              = I2C3;
    hi2c3.Init.Timing           = 0x20B0CCFF;
    hi2c3.Init.OwnAddress1      = 0;
    hi2c3.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    hi2c3.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
    hi2c3.Init.OwnAddress2      = 0;
    hi2c3.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c3.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
    hi2c3.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;
    if(HAL_I2C_Init(&hi2c3) != HAL_OK)
    {
        // Error_Handler();
    }

    /** Configure Analogue filter
     */
    if(HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
    {
        // Error_Handler();
    }

    /** Configure Digital filter
     */
    if(HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
    {
        // Error_Handler();
    }
    /* USER CODE BEGIN I2C3_Init 2 */

    /* USER CODE END I2C3_Init 2 */
}